package ro.imc.ums.domnica;

public class Person {
	private String firstName;
	private String lastName;
	private int id;
	private String cnp;
	
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	
	public String getCnp() {
		return cnp;
	}
	public void setCnp(String cnp) {
		this.cnp = cnp;
	}
	public Person(){
		
	}
	public Person(String firstName, String lastName, int id, String cnp){
		this.firstName = firstName;
		this.lastName = lastName;
		this.id = id;  
		this.cnp = cnp;
	}
	

    public static int[] getArray(String cnp){
            int[] cnpArray = new int[13];
            
            for(int i=0; i<cnp.length(); i++){
                    cnpArray[i] = Integer.parseInt(String.valueOf(cnp.charAt(i)));
            }
            
            return cnpArray;
    }
    
    public static void is_valid(String cnp) throws CustomFormatException {
            if(cnp.length() == 13){
                    int[] cnpArray = Person.getArray(cnp);
                    
                    long sum = cnpArray[0] * 2 + cnpArray[1] * 7 + cnpArray[2] * 9 + cnpArray[3] * 1 + cnpArray[4] * 4 + cnpArray[5] * 6 
                                     + cnpArray[6] * 3 + cnpArray[7] * 5 + cnpArray[8] * 8 + cnpArray[9] * 2 + cnpArray[10] * 7 + cnpArray[11] * 9;
                    
                    long rest = sum % 11;
                    
                    if(!(((rest < 10) && (rest == cnpArray[12])) || ((rest == 10) && (cnpArray[12] == 1)))) throw new CustomFormatException("Invalid!");
                          
                    
            }
            
          
    }
	
	
} 
